package com.xwh.shop.goods.service.controller;
import com.xwh.shop.common.PageResult;
import com.xwh.shop.common.Result;
import com.xwh.shop.goods.model.entity.Brand;
import com.xwh.shop.goods.service.service.BrandService;
import com.xwh.shop.user.feign.api.UserClient;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xwh
 * @since 2025-07-16
 */
@Slf4j
@RestController
@Tag(name = "品牌管理")
@RequestMapping("/brand")
public class BrandController {
    @Resource
    private BrandService brandService;

    @Resource
    private UserClient userClient;

    @Resource
    private HttpServletRequest request;
    @Operation(summary = "添加品牌", description = "添加品牌")
    @PostMapping("add")
    public Result<Object> add(@RequestBody Brand brand){

        // 调用userClient的checkToken 看看token符合不符合要求
        Result<Boolean> res = userClient.checkToken();
        log.info("brand:{}",brand);
        log.info("result:{}",res);
        this.brandService.save(brand);

        String token = request.getHeader("Authorization");
        log.info("token:{}",token);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Brand> findById(@Parameter(description = "品牌ID") @PathVariable Long id){
        Brand brand = brandService.getById(id);
        return Result.success(brand);
    }

    @PutMapping("/update")
    public Result<Object> update(@RequestBody Brand brand){
        brandService.updateById(brand);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Object> delete(@PathVariable Long id){
        brandService.removeById(id);
        return Result.success();
    }


    // 小程序是用？传数据的，需要用@RequestParam来接收，如果是用JSON传数据 就用DTO来接收
    @GetMapping("/page")
    public PageResult<Brand> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer status) {
        log.info("分页查询品牌列表：current={}, pageSize={}, name={}, status={}", current, pageSize, name, status);

        return PageResult.success(brandService.pageQuery(current, pageSize, name, status));
    }



}
